from PyQt5.Qt import *

class Label(QLabel):
    def autoAddPoint(self,result):
        # 自动添加打码点
        w=(self.width())/4
        h=(self.height()-30)/2
        for index_c in result:
            index_i=int(index_c)
            row=(index_i)//5
            column=(index_i)%4
            centre_x=column*w+0.5*w
            centre_y=row*h+0.5*h+30
            print("打码完成",row,column)
            self.createPoint(QPoint(centre_x, centre_y))




    def clearPoint(self):
        # 清除所有红点按钮
        for child in self.children():
            if child.inherits("QPushButton"):
                child.deleteLater()


    def getResult(self):
        # 通过遍历label控件里面的所有button控件，获取位置，拼接成字符串
        # for child in self.children():
        #     if child.inherits("QPushButton"):
        #         print(child.pos())
        result=",".join(["{},{}".format(child.x()+10,child.y()-20) for child in self.children() if child.inherits("QPushButton")])
        print(result)
        return result

    def mousePressEvent(self, event):
        super(Label, self).mousePressEvent(event)
        print(event.pos())
        if event.x()<=0 or event.y()<=30:
            return None
        self.createPoint(event.pos())


    def createPoint(self,pit):
        point = QPushButton(self)
        point.resize(20, 20)
        point.move(pit - QPoint(10, 10))
        point.setStyleSheet("background-color:red; border-radius:10px;")
        point.show()
        # 监听按钮控件的点击事件，再次点击就会取消
        point.clicked.connect(lambda _, btn=point: btn.deleteLater())

